<!doctype html>
<html>
<head>
	<style type="text/css">
		div {
			padding: 3px;
			margin: 3px;
			float: left;
			border: 3px solid black;
			background: #333;
			color: white;
			-webkit-box-shadow: 3px 3px 5px #555;
		}
		div.done {
			background: #1c5;
		}
	</style>
	<script type="text/javascript" src="jquery.js"></script>
	<script type="text/javascript" src="jquery.tpl.min.js"></script>
	<script type="text/javascript" src="jquery.tpl.block.min.js"></script>
	<script type="text/javascript">
		//************************************ Settings
	
		var $testBlock = 1;
		
		//************************************ Settings - end
		try {console.log()}
		catch (e) {
		window.console = {
			log: function (str) {
			},
			error: function () {
				alert(Array.prototype.join.apply(arguments,[""]));
			},
			info: function (str) {
			}
		};
		}
		var start, benchmarks=[], global=0;
		function _start() {
			start = +new Date;
		}
		function _end() {
			return (+new Date - start);
		}
		
		function run(i , result) {
			i = i || 0;
			
			if (benchmarks[ i ])
				result = benchmarks[ i ]();
			else
				return done();
			
			$('body').append($('<div class="' + (result ? "done": "not") + '">Test # ' + i + '</div>').attr({title:benchmarks[ i ].toString()}).hide().fadeIn(100,function() {
				run(i+1);
			}));
			
			//setTimeout(function() { run(i+1);},0);
		}
		
		function done() {
			$('body').append($('<div class="done">All tests done : ' + global +'</div>').hide().fadeIn(250));
		}
		
		benchmarks.push(function () {
			try {
			console.info("Only string");
			
			_start();
			
			var got = $.template("Only string",{}).html(),
				expected = "Only string";
			
			global+=_end();
			
			if (got === expected) {
				console.log("ok");
				return 1;
			} else
				console.error("error", "Got:",got,"Expected:",expected);
			} catch (e) {
			}
		});
		
		benchmarks.push(function () {
			try {
			console.info("Renaming");
			
			_start();
			
			$.template("wrong",{},"a");
			$.template("right",{},"a");
			
			var got = $.template("a").html(),
				expected = "right";
			
			global+=_end();
			
			if (got === expected) {
				console.log("ok");
				return 1;
			} else
				console.error("error", "Got:",got,"Expected:",expected);
			} catch (e) {
			}
		});
		
		benchmarks.push(function () {
			try {
			console.info("Only eval");
			
			_start();
			
			var got = $.template("{%= 'Only eval' %}",{}).html(),
				expected = "Only eval"; 
			
			global+=_end();
			
			if (got === expected) {
				console.log("ok");
				return 1;
			} else
				console.error("error", "Got:",got,"Expected:",expected);
			} catch (e) {
			}
		});
		
		benchmarks.push(function () {
			try {
			console.info("Only eval variable");
			
			_start();
			
			var got = $.template("{%= a %}",["a"]).html({a:"Only eval variable"}),
				  expected = "Only eval variable";
			
			global+=_end();
			
			if (got === expected) {
				console.log("ok");
				return 1;
			} else
				console.error("error", "Got:",got,"Expected:",expected);
			} catch (e) {
			}
		});
		
		benchmarks.push(function () {
			try {
			console.info("Eval and str");
			
			_start();
			
			var got = $.template("Work{%= ' is ' %}hard",{}).html(),
				expected = "Work is hard";
			
			global+=_end();
			
			if (got === expected) {
				console.log("ok");
				return 1;
			} else
				console.error("error", "Got:",got,"Expected:",expected);
			} catch (e) {
			}
		});
		
		benchmarks.push(function () {
			try {
			console.info("Eval var and str");
			
			_start();
			
			var got = $.template("Work{%= a %}hard",["a"]).html({a: " is "}),
				 expected = "Work is hard"; 
			
			global+=_end();
			
			if ( got === expected) {
				console.log("ok");
				return 1;
			} else
				console.error("error", "Got:",got,"Expected:",expected);
			} catch (e) {
			}
		});

		benchmarks.push(function () {
			try {
			console.info("Simple if");
			
			_start();
			
			var got = $.template("{%if true%}I am ok{%/if%}",{}).html(),
					expected = "I am ok";
			
			global+=_end();
			
			if ( got === expected) {
				console.log("ok");
				return 1;
			} else
				console.error("error", "Got:",got,"Expected:",expected);
			} catch (e) {
			}
		});
		
		benchmarks.push(function () {
			try {
			console.info("If with else");
			
			_start();
			
			var got = $.template("{%if false%}I am not ok{%else%}I am ok{%/if%}",{}).html(),
					expected = "I am ok";
			
			global+=_end();
			
			if ( got === expected) {
				console.log("ok");
				return 1;
			} else
				console.error("error", "Got:",got,"Expected:",expected);
			} catch (e) {
			}
		});
		
		benchmarks.push(function () {
			try {
			console.info("If with elseif");
			
			_start();
			
			var got = $.template("{%if false%}I am not ok{%elseif true%}I am ok{%/if%}",{}).html(),
					expected = "I am ok";
			
			global+=_end();
			
			if ( got === expected) {
				console.log("ok");
				return 1;
			} else
				console.error("error", "Got:",got,"Expected:",expected);
			} catch (e) {
			}
		});
		
		benchmarks.push(function () {
			try {
			console.info("Functions and eval");
			
			_start();
			
			var got = $.template("{%@ test(a) {return a+' ok'} %}{%= test(\"I am\") %}",{}).html(),
					expected = "I am ok";
			
			global+=_end();
			
			if ( got === expected) {
				console.log("ok");
				return 1;
			} else
				console.error("error", "Got:",got,"Expected:",expected);
			} catch (e) {
			}
		});
		
		benchmarks.push(function () {
			try {
			console.info("Scopes");
			
			_start();
			
			var got = $.template("{% var a = 'not in scope'; %}{%~ var a = 'in scope';%}{%= a %}",{}).html(),
					expected = "not in scope";
			
			global+=_end();
			
			if ( got === expected) {
				console.log("ok");
				return 1;
			} else
				console.error("error", "Got:",got,"Expected:",expected);
			} catch (e) {
			}
		});
		
		benchmarks.push(function () {
			try {
			console.info("Each method");
			
			_start();
			
			var got = $.template("{%each a%}{%= this + '-' %}{%/each%}",["a"]).html({a: [1 , 2 , 3]}),
				expected = "1-2-3-";
			
			global+=_end();
			
			if (got === expected) {
				console.log("ok");
				return 1;
			} else
				console.error("error", "Got:",got,"Expected:",expected);
			} catch (e) {}
		});
		
		benchmarks.push(function () {
			try {
			console.info("Catch method");
			
			_start();
			
			var got = $.template("{%catch var a%}I am a{%/catch%}{%= a +' char' %}",{}).html(),
				  expected =  "I am a char";
			
			global+=_end();
			
			if ( got === expected) {
				console.log("ok");
				return 1;
			} else
				console.error("error", "Got:",got,"Expected:",expected);
			} catch (e) {}
		});
		
		benchmarks.push(function () {
			try {
			console.info("Naming templates");
			
			$.template("test",{},"test");
			
			_start();
			
			var got = $.template("test").html(),
				expected = "test";
			
			global+=_end();
			
			if (got === expected) {
				console.log("ok");
				return 1;
			} else
				console.error("error", "Got:",got,"Expected:",expected);
			} catch (e) {}
		});
		
		benchmarks.push(function () {
			try {
			console.info("Naming templates with short hand");
			
			$.template("<div>test</div>",{},"test");
			
			_start();
			
			var got = $.template("<div>{%: test {} %}is hard</div>",{})().html().toLowerCase(),
			expected = "<div>test</div>is hard";
			
			global+=_end();
			
			if (got === expected) {
				console.log("ok");
				return 1;
			} else
				console.error("error", "Got:",got,"Expected:",expected);
			} catch (e) {}
		});
		
		benchmarks.push(function () {
			try {
			console.info("Nested naming and renaming");
			
			_start();
			
			$.template("<div>test</div>",{},"test");
			$.template("<div>{%: test {} %}is hard</div>",{},"kaza");
			
			var got = $.template("<div>{%: kaza {} %}so do it</div>",{})().text().toLowerCase(),
				   expected ="testis hardso do it";
				   
			global+=_end();
			
			if ( got === expected) {
				console.log("ok");
				return 1;
			} else
				console.error("error", "Got:",got,"Expected:",expected);
				
			} catch (e) {}
		});
		
		benchmarks.push(function () {
			console.info("Similar speed test");
			
			var inc_g = "1{%= 2%}3",
				  inc_e = "123",
				  g="",
				  e="";
				
				for (var i=1;i<1000;i++) {
					g+=inc_g;
					e+=inc_e;
				}
				
				if ($.template(g,{}).html() !== e)
					return;
				
				
				i=50;
				_start();
				
				while (i>0) {
					$.template(g,{}).html();
					i--;
				}
				
				i = _end();
				
				global += i;
				
				console.log("Execution time: ", i ,"ms");
				return 1;			
		});
		
		benchmarks.push(function () {
			console.info("Incrementing speed test");
			
			var inc_g = "1{%= 2%}3",
				  inc_e = "123",
				  g="",
				  e="",
				  got;
				
				
				var i = 100;
				
				_start();
				
				while (i>0) {
					if (( got = $.template(g+=inc_g,{}).html()) !== (e+=inc_e))
						return;
					i--;
				}
				
				i= _end();
				
				console.log("Execution time: ", i ,"ms");
				global += i;
				return 1;
			
		});
		
		if ($testBlock) {
			benchmarks.push(function () {
				try {
				console.info("Testing jquery.block.js");
				console.info("Simple");
				_start();
				
				var got = $.template("1{%block 'a'%}2{%/block%}3",{}).html(),
					expected = "123";
				
				global+=_end();
				
				if (got === expected) {
					console.log("ok");
					return 1;
				} else
					console.error("error", "Got:",got,"Expected:",expected);
				} catch (e) {
				console.error(e);
				}
			});
			
			benchmarks.push(function () {
				try {
				console.info("Attachment testing");
				_start();
				
				$.template("<div>1{%block 'a'%}2<{%/block%}3</div>",{},"a");
				
				var got = $.template("{%extends 'a'%}{%block 'a'%}A{%/block%}",{})().html(),
					expected = "1A3";
				
				global+=_end();
				
				if (got === expected) {
					console.log("ok");
					return 1;
				} else
					console.error("error", "Got:",got,"Expected:",expected);
				} catch (e) {
					console.error(e);
				}
			});
			
			benchmarks.push(function () {
				try {
				console.info("Attachment with junk");
				_start();
				
				$.template("<div>1{%block 'a'%}2<{%/block%}3</div>",{},"a");
				
				var got = $.template("{%extends 'a'%}asdfgas{%block 'a'%}A{%/block%}asdfasd",{})().html(),
					expected = "1A3";
				
				global+=_end();
				
				if (got === expected) {
					console.log("ok");
					return 1;
				} else
					console.error("error", "Got:",got,"Expected:",expected);
				} catch (e) {
					console.error(e);
				}
			});
			
			benchmarks.push(function () {
				try {
				console.info("Three different blocks and single attachment");
				_start();
				
				$.template("<div>1{%block 'a'%}2<{%/block%}3{%block 'b'%}4{%/block%}5{%block 'c'%}6{%/block%}7</div>",{},"a");
				
				var got = $.template("{%extends 'a'%}{%block 'a'%}A{%/block%}",{})().html(),
					expected = "1A34567";
				
				global+=_end();
				
				if (got === expected) {
					console.log("ok");
					return 1;
				} else
					console.error("error", "Got:",got,"Expected:",expected);
				} catch (e) {
					console.error(e);
				}
			});
			
			benchmarks.push(function () {
				try {
				console.info("Three different attached blocks");
				_start();
				
				$.template("<div>1{%block 'a'%}2<{%/block%}3{%block 'b'%}4{%/block%}5{%block 'c'%}6{%/block%}7</div>",{},"a");
				
				var got = $.template("{%extends 'a'%}{%block 'a'%}A{%/block%}{%block 'b'%}B{%/block%}{%block 'c'%}C{%/block%}",{})().html(),
					expected = "1A3B5C7";
				
				global+=_end();
				
				if (got === expected) {
					console.log("ok");
					return 1;
				} else
					console.error("error", "Got:",got,"Expected:",expected);
				} catch (e) {
					console.error(e);
				}
			});
			
			benchmarks.push(function () {
				try {
				console.info("Three different attached blocks with junk");
				_start();
				
				$.template("<div>1{%block 'a'%}2{%/block%}3{%block 'b'%}4{%/block%}5{%block 'c'%}6{%/block%}7</div>",{},"a");
				
				var got = $.template("{%extends 'a'%}asdfgasd{%block 'a'%}A{%/block%}asdgs{%block 'b'%}B{%/block%}asgsdgsd{%block 'c'%}C{%/block%}asgasg",{})().html(),
					expected = "1A3B5C7";
				
				global+=_end();
				
				if (got === expected) {
					console.log("ok");
					return 1;
				} else
					console.error("error", "Got:",got,"Expected:",expected);
				} catch (e) {
					console.log(e);
				}
			});
			
			benchmarks.push(function () {
				try {
				console.info("Three repeated blocks");
				_start();
				
				$.template("<div>1{%block 'a'%}2{%/block%}3{%block 'a'%}4{%/block%}5{%block 'a'%}6{%/block%}7</div>",{},"a");
				
				var got = $.template("{%extends 'a'%}",{})().html(),
					expected = "1234567";
				
				global+=_end();
				
				if (got === expected) {
					console.log("ok");
					return 1;
				} else
					console.error("error", "Got:",got,"Expected:",expected);
				} catch (e) {
					console.log(e);
				}
			});
			
			benchmarks.push(function () {
				try {
				console.info("Three repeated blocks with one attachment");
				_start();
				
				$.template("<div>1{%block 'a'%}2{%/block%}3{%block 'a'%}4{%/block%}5{%block 'a'%}6{%/block%}7</div>",{},"a");
				
				var got = $.template("{%extends 'a'%}{%block 'a'%}_{%/block%}",{})().html(),
					expected = "1_3_5_7";
				
				global+=_end();
				
				if (got === expected) {
					console.log("ok");
					return 1;
				} else
					console.error("error", "Got:",got,"Expected:",expected);
				} catch (e) {
					console.log(e);
				}
			});
			
			benchmarks.push(function () {
				try {
				console.info("Three repeated blocks with two attachments");
				_start();
				
				$.template("<div>1{%block 'a'%}2{%/block%}3{%block 'a'%}4{%/block%}5{%block 'a'%}6{%/block%}7</div>",{},"a");
				
				var got = $.template("{%extends 'a'%}{%block 'a'%}_{%/block%}{%block 'a'%}-{%/block%}",{})().html(),
					expected = "1_3-5-7";
				
				global+=_end();
				
				if (got === expected) {
					console.log("ok");
					return 1;
				} else
					console.error("error", "Got:",got,"Expected:",expected);
				} catch (e) {
					console.log(e);
				}
			});
			
			benchmarks.push(function () {
				try {
				console.info("Three repeated blocks with three attachments");
				_start();
				
				$.template("<div>1{%block 'a'%}2{%/block%}3{%block 'a'%}4{%/block%}5{%block 'a'%}6{%/block%}7</div>",{},"a");
				
				var got = $.template("{%extends 'a'%}{%block 'a'%}_{%/block%}{%block 'a'%}-{%/block%}{%block 'a'%}${%/block%}",{})().html(),
					expected = "1_3-5$7";
				
				global+=_end();
				
				if (got === expected) {
					console.log("ok");
					return 1;
				} else
					console.error("error", "Got:",got,"Expected:",expected);
				} catch (e) {
					console.log(e);
				}
			});
			
			benchmarks.push(function () {
				try {
				console.info("Three repeated blocks with three attachments and passed vars");
				_start();
				
				$.template("<div>{%= x%}{%block 'a'%}2{%/block%}3{%block 'a'%}4{%/block%}5{%block 'a'%}6{%/block%}{%= x%}</div>",["x"],"a");
				
				var got = $.template("{%extends 'a',{x : '#'}%}{%block 'a'%}_{%/block%}{%block 'a'%}-{%/block%}",{})().html(),
					expected = "#_3-5-#";
				
				global+=_end();
				
				if (got === expected) {
					console.log("ok");
					return 1;
				} else
					console.error("error", "Got:",got,"Expected:",expected);
				
				} catch (e) {
					console.log(e);
				}
			});
			
			benchmarks.push(function () {
				console.info("Similar speed test");
				
				var inc_g = "1{%block 'a'%}2{%/block%}3",
					  inc_e = "123",
					  g="",
					  e="";
					
					for (var i=1;i<400;i++) {
						g+=inc_g;
						e+=inc_e;
					}
					
					if ($.template(g,{}).html() !== e)
						return;
					
					
					i=50;
					_start();
					
					while (i>0) {
						$.template(g,{}).html();
						i--;
					}
					
					i = _end();
					
					global += i;
					
					console.log("Execution time: ", i ,"ms");
					return 1;			
			});
			
			benchmarks.push(function () {
				console.info("Incrementing speed test");
				
				var inc_g = "1{%block 'a'%}2{%/block%}3",
					  inc_e = "123",
					  g="",
					  e="",
					  got;
					
					
					var i = 70;
					
					_start();
					
					while (i>0) {
						if (( got = $.template(g+=inc_g,{}).html()) !== (e+=inc_e))
							return;
						i--;
					}
					
					i= _end();
					
					console.log("Execution time: ", i ,"ms");
					global += i;
					return 1;
				
			});
			
			benchmarks.push(function () {
				console.info("Similar speed test with attachments");
				
				var
					  inc_e = "123",
					  g1="<div>",
					  g2="{%extends 'a'%}",
					  e="";
					
					for (var i=1;i<100;i++) {
						g1+="1{%block 'a"+i+"'%}2{%/block%}3";
						g2+="{%block 'a"+i+"'%}2{%/block%}";
						e+=inc_e;
					}
					g1+="</div>";
					
					$.template(g1, {}, "a");
					
					if (( g1 = $.template(g2,{})().html()) !== e)
						return console.log(g1);
					
					
					i=70;
					_start();
					
					while (i>0) {
						$.template(g2,{}).html();
						i--;
					}
					
					i = _end();
					
					global += i;
					
					console.log("Execution time: ", i ,"ms");
					return 1;			
			});
		}
		
		run(0);
	</script>
	<title>jQuery.tpl test case</title>
</head>
<body>

</body>
</html>